namespace _1._12._2015_TodoWebAPI.Data.Migrations
{
    using _1._12._2015_TodoWebAPI.Data.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    internal sealed class Configuration : DbMigrationsConfiguration<_1._12._2015_TodoWebAPI.Data.ApplicationDbContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(_1._12._2015_TodoWebAPI.Data.ApplicationDbContext context)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            ApplicationUser nick = userManager.FindByName("nick@codercamps.com");
            ApplicationUser coral = userManager.FindByName("coral@email.com");

            if (nick == null)
            {
                nick = new ApplicationUser
                {
                    UserName = "nick@codercamps.com",
                    Email = "nick@codercamps.com"
                };

                userManager.Create(nick, "123456");

                nick = userManager.FindByName("nick@codercamps.com");
            }

            if (coral == null)
            {
                coral = new ApplicationUser
                {
                    UserName = "coral@email.com",
                    Email = "coral@email.com"
                };

                userManager.Create(coral, "123456");

                coral = userManager.FindByName("coral@email.com");
            }

            context.Todos.AddOrUpdate(t => t.Text,
                new Todo
                {
                    City = "Houston",
                    Text = "Make this work with Web API",
                    User = nick,
                    CreatedUser = nick,
                    UpdatedUser = nick,
                    DateCreated = DateTime.Now,
                    DateUpdated = DateTime.Now
                },
                new Todo
                {
                    City = "New York",
                    Text = "Are you working?",
                    User = coral,
                    CreatedUser = coral,
                    UpdatedUser = coral,
                    DateCreated = DateTime.Now,
                    DateUpdated = DateTime.Now
                });

            context.SaveChanges();
            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}
